package daily;

import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream;
import org.apache.commons.compress.utils.IOUtils;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.concurrent.TimeUnit;

public class TestDownloadFile {
    public static void main(String[] args) throws Exception {
        // 解压示例
        TimeUnit.SECONDS.sleep(30);
        String sourceTarGzFile = "D:\\pdf.tar.gz";
        String destinationFolder = "D:\\tmp";
        System.out.println("开始");
        decompressTarGz(sourceTarGzFile, destinationFolder);
        System.out.println("结束");

        // 压缩示例
//        String sourceFolder = "source_folder";
//        String destinationTarGzFile = "compressed_example.tar.gz";
//        compressToTarGz(sourceFolder, destinationTarGzFile);
    }

    public static void decompressTarGz(String sourceFile, String destinationFolder) throws IOException {
        try (FileInputStream fis = new FileInputStream(sourceFile);
             BufferedInputStream bis = new BufferedInputStream(fis);
             GzipCompressorInputStream gzipIn = new GzipCompressorInputStream(bis);
             TarArchiveInputStream tarIn = new TarArchiveInputStream(gzipIn)) {

            TarArchiveEntry entry;
            while ((entry = tarIn.getNextTarEntry()) != null) {
                if (entry.isDirectory()) {
                    continue;
                }
                File outputFile = new File(destinationFolder, entry.getName());
                Files.createDirectories(outputFile.getParentFile().toPath());
                try (OutputStream out = Files.newOutputStream(outputFile.toPath())) {
                    IOUtils.copy(tarIn, out);
                }
            }
        }
    }

    public static void compressToTarGz(String sourceFolder, String destinationFile) throws IOException {
        try (FileOutputStream fos = new FileOutputStream(destinationFile);
             BufferedOutputStream bos = new BufferedOutputStream(fos);
             GzipCompressorOutputStream gzipOut = new GzipCompressorOutputStream(bos);
             TarArchiveOutputStream tarOut = new TarArchiveOutputStream(gzipOut)) {

            Path sourceFolderPath = Paths.get(sourceFolder);
            Files.walk(sourceFolderPath)
                    .filter(path -> !Files.isDirectory(path))
                    .forEach(path -> {
                        TarArchiveEntry tarEntry = new TarArchiveEntry(sourceFolderPath.relativize(path).toString());
                        try (InputStream fis = Files.newInputStream(path)) {
                            tarEntry.setSize(Files.size(path));
                            tarOut.putArchiveEntry(tarEntry);
                            IOUtils.copy(fis, tarOut);
                            tarOut.closeArchiveEntry();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    });
        }
    }
}
